<%-- 
 *Workshop 
 *Class ID        : SE0691   
 *Student ID      : SE90009
 *Student Name    : Phạm Nguyễn Nhật Trung
 *Due Date        : Nov 21, 2012, 11:54:14 PM
 *I declare that this assignment is my own work
 *in accordance with FPT Policy.
--%>

<%@page import="tracker.stub.UsersSessionStub"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.Collection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <LINK REL=StyleSheet HREF="style/style.css" TYPE="text/css" MEDIA=screen>
        <title>Login Page</title>
    </head>
    <body>
        <div class="center-form">
            <h1 style="color: whitesmoke">Account Login Page</h1>
            <div id="login-register" style="">
                <form class="tracker-login-form">
                    <p class="clearfix">
                        <label for="login">Username</label>
                        <input type="text" name="username" id="login" placeholder="Username">
                    </p>
                    <p class="clearfix">
                        <label for="password">Password</label>
                        <input type="password" name="password" id="password" placeholder="Password"> 
                    </p>
                    <p class="clearfix">
                        <input type="button" name="register" value="Register" onclick="parent.location='register.jsp'">
                    </p>
                    <p class="clearfix">
                        <input type="submit" name="submit" value="Login">
                    </p>       
                </form>
            </div>
        </div>

        <%
            String name = "", password = "";
            response.setContentType("text/html;charset=UTF-8");
            try {
                /* TODO output your page here. You may use following sample code. */
                if (request.getParameter("username") != null) {
                    try {
                        name = request.getParameter("username"); //Get input
                        password = request.getParameter("password"); //Get input
                    } catch (Exception e) {
                        out.println("<div style=\"width:800px; margin:0 auto; padding-top: 250px\"><h3>" + e.getMessage() + "</h3></div>");
                    }
                }
            } catch (Exception e) {
                out.println("<div style=\"width:800px; margin:0 auto; padding-top: 250px\"><h3>" + e.getMessage() + "</h3></div>");
            } finally {
                //out.close();
            }
        %>
        <%
            try {
                UsersSessionStub stub = new UsersSessionStub();
                if (stub.checkLoginReturnUserId(name, password) != -1) {
                    Cookie ck = new Cookie("userId", Integer.toString(stub.checkLoginReturnUserId(name, password))); //Write to cookie
                    response.addCookie(ck);
                    ck = new Cookie("userId2", Integer.toString(stub.checkLoginReturnUserId(name, password)));
                    session = request.getSession();
                    ck.setPath(request.getContextPath());
                    response.addCookie(ck);
                    System.out.println("Login Successfully! Username: " + name + " Password: " + password);
                    response.sendRedirect("listTorrents.jsp");
                    return;
                }

            } catch (Exception e) {
                System.out.println("Error in RegisterTag: " + e.getMessage());
            }
        %>
    </body>
</html>
